package net.swissbite.VolumeCallPriceCalc.utils;

/**
 * Copyright (C) 2011  David Daester
 * 
 * This file is part of VolumeCallPriceCalc.
 *
 * VolumeCallPriceCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * VolumeCallPriceCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with VolumeCallPriceCalc.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import net.swissbite.VolumeCallPriceCalc.models.IdDurNumbModel;
import android.database.Cursor;

public class KeyListGenerator {
	private HashMap<String, List<IdDurNumbModel>> numbDur;

	public KeyListGenerator() {

	}

	/**
	 * Updates the data for returning actual data with the getters.
	 * 
	 * @param c
	 *            Allready <b>managed</b> cursor.
	 * @return true if there was any data. False if there was no outgoing calls.
	 */
	public boolean updateData(Cursor c) {

		int dateC = c.getColumnIndex(android.provider.CallLog.Calls.DATE);
		int numbC = c.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
		int durC = c.getColumnIndex(android.provider.CallLog.Calls.DURATION);
		int typeC = c.getColumnIndex(android.provider.CallLog.Calls.TYPE);
		this.numbDur = new HashMap<String, List<IdDurNumbModel>>();
		c.moveToFirst();

		if (c.isFirst()) {

			do {
				if (c.getInt(typeC) == android.provider.CallLog.Calls.OUTGOING_TYPE) {
					Date d = new Date(c.getLong(dateC));
					String identifier = c.getString(numbC);
					if (identifier.startsWith("0")
							&& identifier.charAt(1) != '8') {
						identifier = "+41" + identifier.substring(1);
					}
					long duration = c.getLong(durC);
					if (duration > 0) {
						List<IdDurNumbModel> vals = this.numbDur
								.get(identifier);
						if (vals == null) {
							vals = new LinkedList<IdDurNumbModel>();
							vals.add(new IdDurNumbModel(identifier, d));
							this.numbDur.put(identifier, vals);
						}
						boolean found = false;
						Iterator<IdDurNumbModel> it = vals.iterator();
						IdDurNumbModel val;
						while (!found && it.hasNext()) {
							val = it.next();
							if (d.getYear() == val.getYearsSince1900()
									&& d.getMonth() == val.getMonth()) {
								found = true;
								val.addDuration(duration);
							}
						}
						if (!found) {
							val = new IdDurNumbModel(identifier, d);
							val.addDuration(duration);
							vals.add(val);
						}
					}
				}
			} while (c.moveToNext());
			return true;
		}
		return false;
	}

	/**
	 * Returns the outgoing calls as a representation of the numbers as Key and
	 * the data model in list for the specific grouping.
	 * 
	 * @return HashMap with CallerID as Key.
	 */
	public HashMap<String, List<IdDurNumbModel>> getAsNumberMap() {
		return this.numbDur;
	}

	/**
	 * Returns the outgoing calls as a representation for the month (as
	 * {@link Date}) and the data model in list for the specific grouping.
	 * 
	 * @return Hashmap with the Month - as {@link Date} - as key.
	 */
	public HashMap<Date, List<IdDurNumbModel>> getAsDateMap() {
		HashMap<Date, List<IdDurNumbModel>> dateDur = new HashMap<Date, List<IdDurNumbModel>>();
		Iterator<List<IdDurNumbModel>> it = this.numbDur.values().iterator();

		while (it.hasNext()) {
			List<IdDurNumbModel> ldnm = it.next();
			for (IdDurNumbModel data : ldnm) {
				if (!dateDur.containsKey(data.getDate())) {
					dateDur.put(data.getDate(),
							new LinkedList<IdDurNumbModel>());
				}
				dateDur.get(data.getDate()).add(data);
			}
		}
		return dateDur;
	}

	/**
	 * Returns the outgoing calls as a representation for the Calling counts in
	 * a month and the data model in list for the specific grouping.
	 * 
	 * @return Hashmap with the Counts of Calls per as key.
	 */
	public HashMap<Integer, List<IdDurNumbModel>> getAsCountsMap() {
		HashMap<Integer, List<IdDurNumbModel>> countsDur = new HashMap<Integer, List<IdDurNumbModel>>();
		Iterator<List<IdDurNumbModel>> it = this.numbDur.values().iterator();

		while (it.hasNext()) {
			List<IdDurNumbModel> ldnm = it.next();
			for (IdDurNumbModel data : ldnm) {
				if (!countsDur.containsKey(new Integer(data.getCounts()))) {
					countsDur.put(new Integer(data.getCounts()),
							new LinkedList<IdDurNumbModel>());
				}
				countsDur.get(new Integer(data.getCounts())).add(data);
			}
		}
		return countsDur;
	}

	/**
	 * Tells if the dataset is empty (no outgoing calls) or not.
	 * 
	 * @return True if no outgoing calls with a duration longer than 0 seconds,
	 *         false otherwise.
	 */
	public boolean isEmpty() {
		if (numbDur != null && !numbDur.isEmpty())
			return false;
		return true;
	}

}
